package com.facedemo.face;

import com.facedemo.MyApplication;
import com.facedemo.face.callback.LicenseCallback;
import com.facedemo.utils.LogUtils;
import com.facedemo.utils.SharePreferencesUtils;

import net.facelib.live.FaceLiveAndroid;
import net.facelib.mtfsdk.FaceApiMtfV2Android;
import net.gdface.license.LicenseManager;
import net.gdface.license.RegisterException;

//人脸授权管理
public class FaceLicenseManger {
    public static String LICENSE_KEY = "LICENSE_KEY";
    public static String LICENSE_CODE = "SAVE_LICENSE_CODE";

    public static boolean isVaild = false;//是否已经授权
    public static boolean isVaildIng = false;//是否正在授权中

    /**
     * 去在线获取授权码，且授权成功后，直接初始化
     * @param key 授权关键字
     * @param callback 结果回调
     * */
    public static void toGetLicenseOnline(final String key, final LicenseCallback callback) {
        if(isVaildIng) return;

        new Thread(new Runnable() {
            @Override
            public void run() {
                isVaildIng = true;
                LogUtils.printD("LicenseManager 在线获取授权码：key："+key);
                try{
                    LicenseManager manager = FaceApiMtfV2Android.licenseManager();
                    String license = manager.licenseOnline(key);//在线申请授权，获取授权码
                    LogUtils.printD("LicenseManager 获取授权码成功："+license);

                    //人脸SDK授权+初始化
                    manager.installLicense(key, license);
                    isVaild = manager.isValidLicense();
                    if(isVaild) {
                        //保存 授权关键字 和 授权码
                        SharePreferencesUtils.putString(MyApplication.getInstance(),LICENSE_KEY,key);
                        SharePreferencesUtils.putString(MyApplication.getInstance(),LICENSE_CODE,license);

                        FaceLiveAndroid.licenseManager().installLicense(key,license);

                        if(callback!=null) {
                            callback.success(manager.getLicenseKey(),manager.getLicenseCode());
                        }
                    } else {
                        if(callback!=null) {
                            callback.failed("授权初始化失败");
                        }
                    }

                } catch (RegisterException e) {
                    e.printStackTrace();
                    LogUtils.printE("LicenseManager e:"+e.getMessage());
                    if(callback!=null) {
                        callback.failed(e.getMessage());
                    }
                }
                isVaildIng = false;
            }
        }).start();
    }

    /**
     * 如果之前已经成功获取过授权码了，直接初始化
     * 请在子线程中进行
     * @return true:初始化成功  false：没有授权码，没有进行初始化
     * */
    public static boolean toInitLicense() {
        if(isVaildIng) return false;
        if(isVaild) return true;

        //获取之前保存的 授权关键字 和 授权码
        String key = SharePreferencesUtils.getString(MyApplication.getInstance(),LICENSE_KEY);
        key +='\0';
        String license = SharePreferencesUtils.getString(MyApplication.getInstance(),LICENSE_CODE);
        license +='\0';
        if(key!=null && key.length()>0 && license!=null && license.length()>0) {
            LicenseManager manager = FaceApiMtfV2Android.licenseManager();
            //人脸SDK授权+初始化
            manager.installLicense(key, license);
            FaceLiveAndroid.licenseManager().installLicense(key,license);
            isVaild = manager.isValidLicense();
            return isVaild;
        }

        //尚未进行过授权，需要授权
        isVaild = false;
        return false;
    }

}
